package com.zhen.lucene.queries;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.search.vectorhighlight.FieldQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WildcardQueryTest {

    public static void main(String[] args) throws IOException {
        String field = "title";
        Path indexPath = Paths.get("indexdir");
        Directory dir = FSDirectory.open(indexPath);
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher = new IndexSearcher(reader);

        Term term = new Term("content","学*");
        Query wildcardQuery= new WildcardQuery(term);
        System.out.println("Query:" + wildcardQuery);

        FastVectorHighlighter highlighter = new FastVectorHighlighter();
        FieldQuery fieldQuery = highlighter.getFieldQuery(wildcardQuery);

        TopDocs tds = searcher.search(wildcardQuery,10);
        for(ScoreDoc sd : tds.scoreDocs){
            Document doc = searcher.doc(sd.doc);
            System.out.println("DocID:" + sd.doc);
            System.out.println("id:" + doc.get("id"));
            System.out.println("title:" + doc.get("title"));
            System.out.println("文档评分:" + sd.score);

            String[] frags = highlighter.getBestFragments(fieldQuery,reader,sd.doc,field,100,3);
            for(String frag : frags){
                System.out.println("        " + frag);
            }
        }

        dir.close();
        reader.close();
    }

}
